package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataActionScale extends DataAction implements ITag{
	public static String DEFAULT_ID="_ActionScale";
		DataScale startFactor = new DataScale(1);
		DataScale factor = new DataScale(1);
		DataScale endFactor = new DataScale(2);
public DataActionScale(DataNode parent) {
	super(parent);

	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{
	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START_FACTOR))
		{
			startFactor = new DataScale(1);
			startFactor.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.FACTOR))
		{
			factor = new DataScale(1);
			factor.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.END_FACTOR))
		{
			endFactor = new DataScale(2);
			endFactor.buildFromgrfxML(element);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_SCALE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START_FACTOR))
		{
			startFactor.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.FACTOR))
		{
			factor.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.END_FACTOR))
		{
			endFactor.copyFromgrfxML(element);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_SCALE,""+element.getStartLineNo()} );
		}
	}
}
public DataAction getClone() {

	DataActionScale newActionScale = new DataActionScale(parent);

	super.getClonedElements(newActionScale);

	newActionScale.startFactor = startFactor.getClone();
	newActionScale.factor = factor.getClone();
	newActionScale.endFactor = endFactor.getClone();

	return newActionScale;
}
public DataAction getClone(String path) {

	DataActionScale newActionScale = new DataActionScale(parent);

	GrfxMLStoreProcessor.setCloneName( path,this,newActionScale );

	super.getClonedElements(path,newActionScale);

	newActionScale.startFactor = startFactor.getClone(path);
	newActionScale.factor = factor.getClone(path);
	newActionScale.endFactor = endFactor.getClone(path);

	return newActionScale;
}
public DataScale getEndFactor() {
	return endFactor;
}
public DataScale getFactor() {
	return factor;
}
public DataScale getStartFactor() {
	return startFactor;
}
/**
 * process method comment.
 */
protected void process()
{
 try{
	double newFactor = startFactor.getValue();
	double diffFactor = endFactor.getValue() - newFactor;
	newFactor += currentStep.getValue() * diffFactor / steps.getValue();
	factor.setValue((float) newFactor, this);
 }catch(LockException le){}
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(startFactor,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(factor,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(endFactor,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {

	replace(oldObj,newObj,false);

}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	super.replace(oldObj,newObj);

	if(startFactor==oldObj)
		startFactor=(DataScale)newObj;

	if(factor==oldObj)
		factor=(DataScale)newObj;

	if(endFactor==oldObj)
		endFactor=(DataScale)newObj;
}
public void resetChanged() {
	super.resetChanged();
	startFactor.resetChanged();
	factor.resetChanged();
	endFactor.resetChanged();
}
protected void setEndFactor(DataScale newEndFactor) {
	endFactor = newEndFactor;
}
protected void setFactor(DataScale newFactor) {
	factor = newFactor;
}
protected void setStartFactor(DataScale newStartFactor) {
	startFactor = newStartFactor;
}
public String togrfxML(String name) {

	String xml = "<"+name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";

		xml+=super.togrfxML(name);
		xml+=startFactor.togrfxML(new DataScale(1),Token.START_FACTOR);
		xml+=factor.togrfxML(new DataScale(1),Token.FACTOR);
		xml+=endFactor.togrfxML(new DataScale(2),Token.END_FACTOR);

	xml+="</"+name+">\n";

	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
